package com.xqboss.web.controller.marketing;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xqboss.apps.domain.marketing.MarketingAirdrop;
import com.xqboss.apps.domain.marketing.MarketingAirdropMaterial;
import com.xqboss.apps.domain.marketing.MarketingAirdropMaterialRecord;
import com.xqboss.apps.domain.sys.SnapshotCollection;
import com.xqboss.apps.dto.marketing.AddAirdropDto;
import com.xqboss.apps.dto.marketing.GetAirdropListDto;
import com.xqboss.apps.dto.marketing.GetAirdropRecordListDto;
import com.xqboss.apps.dto.marketing.WebAirdropMaterialDto;
import com.xqboss.apps.dto.sys.SnapshotDto;
import com.xqboss.apps.service.marketing.AirdropRecordService;
import com.xqboss.apps.service.marketing.AirdropService;
import com.xqboss.apps.service.marketing.MarketingAirdropMaterialRecordService;
import com.xqboss.apps.service.marketing.MarketingAirdropMaterialService;
import com.xqboss.apps.service.sys.SnapshotCollectionService;
import com.xqboss.apps.vo.marketing.AirdropMaterialRecordExportVo;
import com.xqboss.apps.vo.marketing.AirdropRecordExportVo;
import com.xqboss.apps.vo.marketing.AirdropRecordVo;
import com.xqboss.common.annotation.Log;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.enums.BusinessType;
import com.xqboss.common.utils.StringUtils;
import com.xqboss.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author Trent
 * @date 2023/9/5
 */
@Api(tags = "营销-空投管理")
@RestController("WebAirdropController")
@RequestMapping("/web/airdrop")
public class AirdropController {

    @Autowired
    private AirdropService airdropService;

    @Autowired
    private AirdropRecordService airdropRecordService;
    @Autowired
    private SnapshotCollectionService snapshotCollectionService;
    @Autowired
    private MarketingAirdropMaterialService marketingAirdropMaterialService;
    @Autowired
    private MarketingAirdropMaterialRecordService marketingAirdropMaterialRecordService;

    @PreAuthorize("@ss.hasPermi('web:airdrop:list')")
    @ApiOperation("获取空投列表")
    @GetMapping("list")
    public R<List<MarketingAirdrop>> list(@Validated GetAirdropListDto params) {
        if(StringUtils.isEmpty(params.getOrderByColumn())){
            params.setOrderByColumn("id");
            params.setSortAsc(false);
        }
        params.startPageAndOrder();
        List<MarketingAirdrop> airdropList = airdropService.getAirdropList(params.getSearch());
        return R.ok(airdropList);
    }

    @PreAuthorize("@ss.hasPermi('web:airdrop:detail')")
    @ApiOperation("获取单个空投信息")
    @GetMapping("detail/{airdropId}")
    public R<MarketingAirdrop> detail(@Validated @PathVariable("airdropId")
                                                @ApiParam(value = "空投ID", required = true)
                                                @NotNull(message = "空投ID不能为空")
                                                Long airdropId) {
        MarketingAirdrop airdrop = airdropService.getAirdropDetail(airdropId);
        return R.ok(airdrop);
    }

    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:airdrop:add')")
    @ApiOperation("添加空投")
    @PostMapping("add")
    @Log(title = "空投", businessType = BusinessType.INSERT)
    public R add(@Validated @RequestBody AddAirdropDto params) {
        airdropService.addAirdrop(params);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:airdrop:user:list')")
    @ApiOperation("获取空投详情列表")
    @GetMapping("user/list")
    public R<List<AirdropRecordVo>> attrList(@Validated GetAirdropRecordListDto params) {
        params.startPageAndOrder();
        List<AirdropRecordVo> voList = airdropRecordService.getRecordByAirdropId(params);
        return R.ok(voList);
    }

    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:snapshot:create')")
    @ApiOperation("创建快照")
    @PostMapping("snapshot/create")
    @Log(title = "快照", businessType = BusinessType.INSERT)
    public R create(@Validated @RequestBody SnapshotDto dto) {
        return snapshotCollectionService.create(dto);
    }

    @PreAuthorize("@ss.hasPermi('web:snapshot:list')")
    @ApiOperation("快照列表")
    @PostMapping("snapshot/list")
    public R<List<SnapshotCollection>> snapshotList(@RequestBody SnapshotDto dto) {
        LambdaQueryWrapper<SnapshotCollection> wq = new LambdaQueryWrapper<>();
        if (ObjUtil.isNotNull(dto.getType())) {
            wq.eq(SnapshotCollection::getType, dto.getType());
        }
        if (StrUtil.isNotBlank(dto.getSearch())) {
            wq.like(SnapshotCollection::getSnapshotCause, dto.getSearch());
        }
        wq.orderByDesc(SnapshotCollection::getCreateTime);
        dto.startPageAndOrder();
        List<SnapshotCollection> list = snapshotCollectionService.list(wq);
        return R.ok(list);
    }

    @PreAuthorize("@ss.hasPermi('web:airdrop:export')")
    @ApiOperation("导出空投记录")
    @PostMapping("export")
    public void export(@Validated GetAirdropRecordListDto params, HttpServletResponse response) {
        List<AirdropRecordExportVo> voList = airdropRecordService.export(params);
        ExcelUtil<AirdropRecordExportVo> util = new ExcelUtil<>(AirdropRecordExportVo.class);
        util.exportExcel(response, voList, "空投记录详情");
    }

    /**
     * ================================================================================================================
     * v1.7.1以下为批量空投-材料相关接口
     * ================================================================================================================
     */
    @RepeatSubmit(interval = 1000, justUrl = true)
    @PreAuthorize("@ss.hasPermi('web:airdrop:material:add')")
    @ApiOperation("空投-材料新增")
    @PostMapping("material/add")
    @Log(title = "空投-材料", businessType = BusinessType.INSERT)
    public R materialAdd(@Validated @RequestBody WebAirdropMaterialDto params) {
        airdropService.addAirdropMaterial(params);
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('web:airdrop:material:list')")
    @ApiOperation("空投-材料列表")
    @GetMapping("material/list")
    public R<List<MarketingAirdropMaterial>> materialList(@Validated GetAirdropListDto params) {
        params.startPage();
        List<MarketingAirdropMaterial> airdropList = marketingAirdropMaterialService.getAirdropMaterialList(params);
        return R.ok(airdropList);
    }

    @PreAuthorize("@ss.hasPermi('web:airdrop:material:record')")
    @ApiOperation("空投-材料详情")
    @GetMapping("material/record/{airdropId}")
    public R<List<MarketingAirdropMaterialRecord>> materialRecord(@PathVariable Long airdropId) {
        List<MarketingAirdropMaterialRecord> airdropList = marketingAirdropMaterialRecordService.getAirdropMaterialRecord(airdropId);
        return R.ok(airdropList);
    }

    @PreAuthorize("@ss.hasPermi('web:airdrop:material:export')")
    @ApiOperation("空投-材料导出")
    @PostMapping("material/export")
    public void materialExport(@Validated GetAirdropRecordListDto params, HttpServletResponse response) {
        List<AirdropMaterialRecordExportVo> voList = marketingAirdropMaterialRecordService.export(params);
        ExcelUtil<AirdropMaterialRecordExportVo> util = new ExcelUtil<>(AirdropMaterialRecordExportVo.class);
        util.exportExcel(response, voList, "空投-材料记录详情");
    }
}
